module.exports = {
  /**
   * @description 格式化日期
   * @param {*} value 值
   * @param {*} fmt 格式
   * @param {*} emptyText
   * @returns 转换后的值
   */
  formatDate(value, fmt, emptyText) {
    fmt = fmt || "yyyy-MM-dd hh:mm:ss";
    if (!value) return emptyText || "";
    let getDate = new Date(value);
    let o = {
      "M+": getDate.getMonth() + 1,
      "d+": getDate.getDate(),
      "h+": getDate.getHours(),
      "m+": getDate.getMinutes(),
      "s+": getDate.getSeconds(),
      "q+": Math.floor((getDate.getMonth() + 3) / 3),
      S: getDate.getMilliseconds(),
    };
    if (/(y+)/.test(fmt)) {
      fmt = fmt.replace(
        RegExp.$1,
        (getDate.getFullYear() + "").substr(4 - RegExp.$1.length)
      );
    }
    for (let k in o) {
      if (new RegExp("(" + k + ")").test(fmt)) {
        fmt = fmt.replace(
          RegExp.$1,
          RegExp.$1.length === 1
            ? o[k]
            : ("00" + o[k]).substr(("" + o[k]).length)
        );
      }
    }
    return fmt;
  },
  /**
   * @description 转换数字
   * @param {String | Number} value 转换值
   * @param {String | Number} fixed 保留位数
   * @returns { String } 转换后的值
   */
  toY(value, fixed) {
    if (!value || isNaN(value)) {
      return 0;
    }
    let isMinus = false;
    let param = {};
    let k = 10000,
      sizes = ["", "万", "亿", "万亿"],
      i;
    if (value < 0) {
      isMinus = true;
      value = -value;
    }
    if (value < k) {
      param.value = value;
      param.unit = "";
    } else {
      i = Math.floor(Math.log(value) / Math.log(k));
      param.value = value / Math.pow(k, i);
      param.unit = sizes[i];
    }
    if (
      param.value &&
      param.value.toString().split(".")[1] &&
      param.value.toString().split(".")[1].length > fixed
    ) {
      param.value = Number(param.value).toFixed(fixed);
    }
    if (isMinus) {
      return -param.value + param.unit;
    }
    return param.value + param.unit;
  },
  /**
   * @description 判断是否为Object
   * @param {*} value
   * @returns { Boolean }
   */
  isObject(value) {
    const type = typeof value;
    return value !== null && (type === "object" || type === "function");
  },
  /**
   * @description 深度合并对象
   * @param {Object} source 基础对象
   * @param {Object} other 添加对象
   * @returns 新对象
   */
  merge(source, other) {
    if (!this.isObject(source) || !this.isObject(other)) {
      return other === undefined ? source : other;
    }
    return Object.keys({
      ...source,
      ...other,
    }).reduce(
      (acc, key) => {
        acc[key] = this.merge(source[key], other[key]);
        return acc;
      },
      Array.isArray(source) ? [] : {}
    );
  },

  judgeType(obj) {
    const toString = Object.prototype.toString;
    const map = {
      "[object Boolean]": "boolean",
      "[object Number]": "number",
      "[object String]": "string",
      "[object Function]": "function",
      "[object Array]": "array",
      "[object Date]": "date",
      "[object RegExp]": "regExp",
      "[object Undefined]": "undefined",
      "[object Null]": "null",
      "[object Object]": "object",
    };
    if (obj instanceof Element) {
      return "element";
    }
    return map[toString.call(obj)];
  },
  /**
   * @description 深克隆
   * @param {Object} data 
   */
  deepClone(data) {
    const type = this.judgeType(data);
    let obj = null;
    if (type == "array") {
      obj = [];
      for (let i = 0; i < data.length; i++) {
        obj.push(this.deepClone(data[i]));
      }
    } else if (type == "object") {
      obj = {};
      for (let key in data) {
        if (data.hasOwnProperty(key)) {
          obj[key] = this.deepClone(data[key]);
        }
      }
    } else {
      return data;
    }
    return obj;
  },
  /**
   * @description 二次封装typeof方法
   * @param {*} val 
   */
  myTypeOf(val) {
    var type = typeof val;
    if (val === null) {
      return "null";
    } else if (type === "object") {
      return this.judgeType(val);
    } else {
      return type;
    }
  },
  /**
   * @description 原始值数组去重
   * @param {Array} arr 
   */
  unique(arr){
    var obj = {},newArr = [],i = 0
    for(i;i<arr.length;i++){
      if(!obj.hasOwnProperty(arr[i])){
        obj[arr[i]] = arr[i]
        newArr.push(arr[i])
      }
    }
    return newArr
  }
};
